package com.software.test;

import com.software.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.Date;
import java.util.UUID;

public class Run {
    public static void main(String[] args) {
        // 加载hibernate.cfg.xml
        Configuration cfg = new Configuration();
        cfg.configure();
        // 获取session 相当于获取了connection
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        Session session = sessionFactory.openSession();
        // 开启事务
        Transaction transaction = session.beginTransaction();

        // 相当于SQL
        User user = new User();
        user.setUserId(UUID.randomUUID().toString());
        user.setUserName("hugx");
        user.setPassword("hugx");
        user.setLoginTime(new Date());

        // 保存，相当于执行SQL
        session.save(user);
        // 提交事务
        transaction.commit();

        // 关闭资源
        session.close();
        sessionFactory.close();

    }
}
